package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.asvp_util.enumerative.Role;
import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.dao.MemberAccountDao;
import it.asvp.asvp_portal.data.dao.MemberDao;
import it.asvp.asvp_portal.data.dao.NotificationDao;
import it.asvp.asvp_portal.data.entity.member.MemberAccount;
import it.asvp.asvp_portal.data.entity.notification.Notification;
import it.asvp.asvp_portal.data.entity.notification.NotificationReceiver;
import it.asvp.asvp_portal.service.util.EmailSender;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

@Configuration
@PropertySource("classpath:messages.properties")
@Service
public class TaskService {

//	@Autowired
//	MemberDao memberDao;
//	@Autowired
//	NotificationDao notificationDao;
//	@Autowired
//	MemberAccountDao memberAccountDao;
//	@Autowired
//	EmailSender emailSender;
//	
//	@Value("${account.in.scadenza.message}") 
//	String expiryAccountMessage;
	
//	@Transactional
//	@Rollback
//	//@Scheduled(fixedDelay = 5000)    // @Scheduled(fixedDelay = 1000*60*60*24)
//	public void sendNotification(){
//		
//		List<Notification> notifications=notificationDao.findByStartDate(DateUtils.getCurrentDate());
//		
//		if(notifications==null) return;
//		
//		for(Notification notification:notifications){
//			
//		   switch(notification.getReceiverOption()){
//		      
//		      case ALCUNI_SOCI : {
//		    	  
//		    	  Set<NotificationReceiver> receivers=notification.getNotificationReceivers();
//		    	  for(NotificationReceiver receiverItem:receivers){
//		    		  String emailAddress=memberDao.findById(receiverItem.getMember().getId()).getEmail();
////		    		  emailSender.sendNotification(notification, emailAddress);
//		    	  }
//		    	  break;
//		      }
//		      
//		      case SOLO_CONSIGLIERI : {
//		    	  
//		    	  List<MemberAccount> councilorAccounts=memberAccountDao.findByRole(Role.ROLE_CONSIGLIERE);
//		    	  for(MemberAccount memberAccountItem:councilorAccounts){
//		    		  String emailAddress=memberDao.findById(memberAccountItem.getMember().getId()).getEmail();
////		    		  emailSender.sendNotification(notification, emailAddress);
//		    	  }
//		    	  break;
//		      }
//		      
//		      case SOLO_SOCI : {
//		    	  List<MemberAccount> memberAccounts=memberAccountDao.findByRole(Role.ROLE_SOCIO);
//		    	  for(MemberAccount memberAccountItem:memberAccounts){
//		    		  String emailAddress=memberDao.findById(memberAccountItem.getMember().getId()).getEmail();
////		    		  emailSender.sendNotification(notification, emailAddress);
//		    	  }
//		    	  System.out.println("***********EMAIL INVIATO AL SOCIO*****************");
//		    	  break;
//		      }
//		      
//		      case TUTTI_SOCI : {
//		    	  
//		    	  List<MemberAccount> allAccounts=memberAccountDao.findAll();
//		    	  for(MemberAccount memberAccountItem:allAccounts){
//		    		  String emailAddress=memberDao.findById(memberAccountItem.getMember().getId()).getEmail();
////		    		  emailSender.sendNotification(notification, emailAddress);
//		    	  }
//		    	  break;
//		      }
//		   }
//		   
//		   Integer frequency=notification.getFrequency();
//		   if(frequency==null || frequency.intValue()==0){
//			   notificationDao.delete(notification);
//		   }else{
//			   //TODO:
//			   //calcola la prossima data di invio e controlla che sia minore o uguale alla data
//			   //di fine della notifica altrimenti cancella la notifica
//		   }
//		}
//	}
	
//	@Transactional
//	@Rollback
//	@Scheduled(cron="*/5 * * * * ?")    // @Scheduled(fixedDelay = 1000*60*60*24)
//    public void adviseBeforeEvent(){
//		System.out.println("**********************JOB SPRING SCHEDULATO OGNI 5 SECONDI*********************");
//	}
    
    //disattiva solo gli account dei soci, quelli dei consiglieri vengono disattivati
    //alla creazione di un nuovo consiglio
	
//	@Transactional
//	@Rollback
//	//@Scheduled(fixedDelay = 5000)   // @Scheduled(fixedDelay = 1000*60*60*24)
//    public void checkAndUpdateAccounts(){
//		
//    	List<MemberAccount> members=memberAccountDao.findByRole(Role.ROLE_SOCIO);
//    	
//    	for(MemberAccount memberAccountItem:members){
//    		
//    		if(DateUtils.compare(memberAccountItem.getExpireDate(), DateUtils.getCurrentDate(), false)<0){
//    			memberAccountItem.setEnabled(false);
//    			memberAccountDao.merge(memberAccountItem);
//    		}else{
//    			//FIXME: se mancano qualche giorno prima della scadenza viene inoltrata una mail al socio
//    			
//    			if(true){
	                //FIXME: true: da sostituire con la differenza di giorni tra la expireDate e la data corrente
//    				String emailAddress=memberDao.findById(memberAccountItem.getMember().getId()).getEmail();
//    				emailSender.sendEmail("scadenza account", expiryAccountMessage, emailAddress);
//    			}
//    		}
//    	}
//	}
}
